#include"CCStack.h"
Stack::Stack() {
    a = NULL;
    capacity = top = 0;
}
Stack::~Stack() {
    free(a);
    a = NULL;
    capacity = top = 0;
}
Stack::Stack(const Stack& st) {
    STDataType* newcopy = (STDataType*)malloc(st.capacity*sizeof(STDataType));
    if (newcopy == NULL) {
        perror("malloc fail");
        return;
    }
    memcpy(newcopy, st.a, sizeof(STDataType*) * st.top);
    capacity = st.capacity;
    top = st.top;
}
void Stack::Push(STDataType x) {
    assert(a);
    if (capacity == top) {
        int newcapacity = capacity == 0 ? 4 : 2 * capacity;
        STDataType* tmp = (STDataType*)realloc(a, newcapacity * sizeof(STDataType));
        if (tmp == NULL) {
            perror("realloc fail");
            return;
        }
        a = tmp;
        capacity = newcapacity;

    }
    a[top] = x;
    top++;
}
void Stack::Pop() {
    assert(a);
    top--;
}
STDataType Stack::Top() {
    assert(a);
    assert(!Empty());
    return a[top - 1];

}
int Stack::Size() {
    assert(a);
    return top;
}
bool Stack::Empty() {
    assert(a);
    return top == 0;
}

